package org.qianye.soft.repository;

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.qianye.soft.entity.Student;
import org.qianye.soft.util.SpringApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoadResultBean;

@Repository
@Transactional
public class StudentRepository {

	private static final Logger log = LoggerFactory.getLogger(StudentRepository.class);

	private SessionFactory sessionFactory;

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Student findById(java.lang.Integer id) {
		log.debug("getting Student instance with id: " + id);
		try {
			return (Student) getCurrentSession().get(Student.class, id);
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Student> findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List<Student> results = getCurrentSession().createCriteria(Student.class).add(create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<Student> findAll() {
		log.debug("finding all Student instances");
		try {
			String queryString = "from findAllorg.qianye.soft.entity.Student";
			Query queryObject = getCurrentSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = (Student) getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void update(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public PagingLoadResult<Student> findAll(PagingLoadConfig loadConfig) {
		PagingLoadResultBean<Student> result = new PagingLoadResultBean<Student>();
		result.setOffset(loadConfig.getOffset());
		log.debug("finding all Param");

		Session session = getCurrentSession();
		Query queryCount = session.createSQLQuery("select count(stu.id) from  Student stu,Clazz cla where stu.claId=cla.id");
		Query queryData = session.createQuery("From org.qianye.soft.entity.Student stu left join fetch stu.clazz");
		Integer totalLength = new Integer(queryCount.uniqueResult().toString());

		queryData.setFirstResult(loadConfig.getOffset());
		queryData.setMaxResults(loadConfig.getLimit());

		result.setTotalLength(totalLength);
		result.setData(queryData.list());

		return result;
	}

	public static StudentRepository getInstance() {
		return SpringApplicationContext.getApplicationContext().getBean(StudentRepository.class);
	}
}
